package com.jlwl.houseservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.ServiceCategory;
import com.jlwl.houseservice.service.ServiceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 服务分类控制器
 */
@RestController
@RequestMapping("/service-categories")
public class ServiceCategoryController {

    @Autowired
    private ServiceCategoryService serviceCategoryService;

    /**
     * 获取服务分类列表（分页）
     */
    @GetMapping
    public Result<Page<ServiceCategory>> list(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "status", required = false) Integer status) {
        
        LambdaQueryWrapper<ServiceCategory> queryWrapper = new LambdaQueryWrapper<>();
        if (status != null) {
            queryWrapper.eq(ServiceCategory::getStatus, status);
        }
        queryWrapper.orderByAsc(ServiceCategory::getDisplayOrder);
        
        Page<ServiceCategory> pageResult = serviceCategoryService.page(new Page<>(page, size), queryWrapper);
        return Result.success(pageResult);
    }

    /**
     * 获取所有启用的服务分类
     */
    @GetMapping("/all")
    public Result<List<ServiceCategory>> listAll() {
        List<ServiceCategory> list = serviceCategoryService.list(
                new LambdaQueryWrapper<ServiceCategory>()
                        .eq(ServiceCategory::getStatus, 1)
                        .orderByAsc(ServiceCategory::getDisplayOrder)
        );
        return Result.success(list);
    }

    /**
     * 获取服务分类详情
     */
    @GetMapping("/{id}")
    public Result<ServiceCategory> getById(@PathVariable Integer id) {
        ServiceCategory serviceCategory = serviceCategoryService.getById(id);
        if (serviceCategory == null) {
            return Result.error("服务分类不存在");
        }
        return Result.success(serviceCategory);
    }

    /**
     * 添加服务分类
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<ServiceCategory> add(@RequestBody ServiceCategory serviceCategory) {
        serviceCategory.setCreatedAt(LocalDateTime.now());
        serviceCategory.setUpdatedAt(LocalDateTime.now());
        serviceCategoryService.save(serviceCategory);
        return Result.success(serviceCategory);
    }

    /**
     * 更新服务分类
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> update(@PathVariable Integer id, @RequestBody ServiceCategory serviceCategory) {
        ServiceCategory existCategory = serviceCategoryService.getById(id);
        if (existCategory == null) {
            return Result.error("服务分类不存在");
        }
        
        serviceCategory.setId(id);
        serviceCategory.setUpdatedAt(LocalDateTime.now());
        serviceCategoryService.updateById(serviceCategory);
        return Result.success();
    }

    /**
     * 删除服务分类
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> delete(@PathVariable Integer id) {
        ServiceCategory existCategory = serviceCategoryService.getById(id);
        if (existCategory == null) {
            return Result.error("服务分类不存在");
        }
        
        serviceCategoryService.removeById(id);
        return Result.success();
    }

    /**
     * 更新服务分类状态
     */
    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        ServiceCategory existCategory = serviceCategoryService.getById(id);
        if (existCategory == null) {
            return Result.error("服务分类不存在");
        }
        
        ServiceCategory updateCategory = new ServiceCategory();
        updateCategory.setId(id);
        updateCategory.setStatus(status);
        updateCategory.setUpdatedAt(LocalDateTime.now());
        serviceCategoryService.updateById(updateCategory);
        return Result.success();
    }
} 